import store from '../store/index'
const preMusic = 'xiaou-bookmark-'
// 更新兼容
// 同步用户
// const syncKey = ['setting', 'command', 'user']
// 普通用户
const comKey = ['oss', 'image', 'configure']

function rubickUtils () {}
rubickUtils.save = (...keys) => {
  keys.map((key) => {
    const data = store.state[key]
    update(key, data)
  })
}
rubickUtils.readAll = () => {
  console.log('load....')
  rubickUtils.read(...comKey)
}
rubickUtils.read = (...keys) => {
  keys.map((key) => {
    const data = read(key)
    if (data) {
      store.state[key] = { ...store.state[key], ...data }
      console.log(key, store.state[key])
    }
  })
}
/**
 * 删除 key
 */
// eslint-disable-next-line no-unused-vars
function deleteItem (key) {
  // eslint-disable-next-line no-undef
  rubick.db.remove(`${preMusic}${key}`)
}
/**
 * 获得 数据
 */
function read (key, onlyData = true) {
  // eslint-disable-next-line no-undef
  const data = rubick.db.get(`${preMusic}${key}`)
  console.log(data)
  if (!data) {
    return false
  }
  if (onlyData) {
    return data.data
  } else {
    return data
  }
}
/**
 * 更新数据
 */
function update (key, data) {
  const readData = read(key, false)
  let res
  if (!readData) {
    // eslint-disable-next-line no-undef
    res = rubick.db.put({
      _id: `${preMusic}${key}`,
      data: data,
      _rev: readData._rev
    })
  } else {
    // eslint-disable-next-line no-undef
    res = rubick.db.put({
      _id: `${preMusic}${key}`,
      data: data,
      _rev: readData._rev
    })
  }
  console.log('update' + res.toString())
}

window.saveALL = () => {
  console.log('保存')
  rubickUtils.save(...comKey)
}
rubickUtils.playPromptTone = (fileName, pre = false) => {
  const audio = new Audio()
  audio.loop = false
  if (pre) {
    audio.play()
    let fig = true
    audio.addEventListener('ended', function () {
      if (fig) {
        audio.src = `https://xiaou520.gitee.io/sound/${fileName}`
        audio.play()
        fig = false
      }
    }, false)
  } else {
    audio.src = `https://xiaou520.gitee.io/sound/${fileName}`
    audio.play()
  }
}
rubickUtils.isNewVersion = () => {
  // 当前版本
  const pluginInfo = window.pluginInfo
  console.log(pluginInfo)
  // rubick 自动更新数据处理
  if (read('version') !== pluginInfo.version) {
    // rubickUtils.playPromptTone('update.wav')
    // 是否需要更新数据
    if (window.pluginInfo.update) {
      console.log('更新数据')
      // const oldOss = read('oss')
      // if (oldOss) {
      //   const oldOsskeys = oldOss
      //   const newOssKeys = store.state.oss
      //   newOssKeys.aliOss = oldOsskeys.aliOss
      //   newOssKeys.tencentOss = oldOsskeys.tencentOss
      //   newOssKeys.rruu = oldOsskeys.rruu
      //   if (newOssKeys.tencentOss.domain === undefined) {
      //     newOssKeys.tencentOss.domain = ''
      //   }
      //   if (newOssKeys.tencentOss.style === undefined) {
      //     newOssKeys.tencentOss.style = ''
      //   }
      //   if (oldOsskeys.GitHub) {
      //     newOssKeys.GitHub = oldOsskeys.GitHub
      //   }
      //   if (oldOsskeys.smMs) {
      //     newOssKeys.smMs = oldOsskeys.smMs
      //   }
      //   if (!oldOss.aliOss.uploadDirectory) {
      //     newOssKeys.aliOss.uploadDirectory = ''
      //   }
      //   if (oldOss.GitHub.branch === undefined) {
      //     newOssKeys.GitHub.branch = ''
      //     if (oldOss.GitHub.at === false || oldOss.GitHub.at === true) {
      //       delete newOssKeys.GitHub.at
      //     }
      //   }
      //   if (oldOss.tencentOss.path === undefined) {
      //     newOssKeys.tencentOss.path = ''
      //   }
      //   rubickUtils.save('oss')
      // }
    }
    update('version', pluginInfo.version)
    // 打开更新信息窗口
    // window.createUpdateWindow()
    return true
  } else {
    console.log('false')
    return false
  }
}
export default rubickUtils
